package com.bidevalution.business.integrate;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.springframework.util.StringUtils;

import com.bidevalution.business.factory.StepRowJobFactory;
import com.bidevalution.business.function.FunctionCalculator;
import com.bidevalution.business.service.JdbcTemplateService;
import com.bidevalution.dao.entity.BeSectionStepJob;
import com.bidevalution.support.exception.BizException;
import com.bidevalution.support.util.LogUtil;
import com.bidevalution.support.util.OConvertUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 行数据执行任务
 * 
 * @author daizhifeng
 * @date 2019年11月25日 下午4:26:44
 * @version V2.0
 */
public class StepRowFormulaJob implements StepRowJobFactory {
	private Logger log = Logger.getLogger(StepRowFormulaJob.class);

	@Override
	public Boolean execute(BeSectionStepJob beSectionStepJob, String submitCode,String expertId, Map<String, Object> rowData) throws Exception {
		String formulaConfig = beSectionStepJob.getJobContent();
		if (!StringUtils.isEmpty(formulaConfig)) {
			Map<String, Object> envMap = Maps.newHashMap();
			for (Entry<String, Object> entry : rowData.entrySet()) {
				envMap.put(entry.getKey(), entry.getValue());
			}
			envMap.put("$sectionStepId", beSectionStepJob.getSectionStepId());
			envMap.put("$submitCode", submitCode);
			envMap.put("$sectionId", beSectionStepJob.getSectionId());
			envMap.put("$layoutCode", beSectionStepJob.getLayoutCode());
			formulaConfig = formulaConfig.trim();
			if (formulaConfig.toLowerCase().startsWith("{ael[")) {
				formulaConfig = formulaConfig.substring(5, formulaConfig.length() - 2);
				log.info("计算公式公式:" + formulaConfig);
				FunctionCalculator.computeAviator(formulaConfig, envMap);
			} else if (formulaConfig.toLowerCase().startsWith("{mel[")) {
				formulaConfig = formulaConfig.substring(5, formulaConfig.length() - 2);
				log.info("计算公式公式:" + formulaConfig);
				FunctionCalculator.computeMVEL(formulaConfig, envMap);
			} else
				throw new BizException("计算公式格式错误:" + formulaConfig);
			return true;
		}
		return false;
	}

	@Override
	public void setJdbcTemplateService(JdbcTemplateService jdbcTemplateService) {
		
	}

}
